package com.scan.web;

import java.io.IOException;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import utils.WeixinTools;
import com.scan.config.WxProperties;

/**
 * Created by tancw on 2016/10/8.
 */
@Controller
public class WeixinServelt {

	@Autowired
	private WxProperties properties;

	@RequestMapping(value = "weixin.page",method = RequestMethod.GET)
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			String signature = Objects.requireNonNull(request.getParameter("signature"));
			String timestamp = Objects.requireNonNull(request.getParameter("timestamp"));
			String nonce = Objects.requireNonNull(request.getParameter("nonce"));
			String echostr = Objects.requireNonNull(request.getParameter("echostr"));

			if (WeixinTools.access(properties.getToken(), signature, timestamp, nonce)) {
				response.getWriter().write(echostr);
			}
		} catch (NullPointerException e) {
			return;
		}
	}

//	@RequestMapping(method = RequestMethod.POST)
//	public void doPost(HttpServletRequest request, HttpServletResponse response) {
//		Token token = TokenAPI.get(properties.getAppID(), properties.getAppsecret());
//		System.out.println(token);
//	}

}
